package com.shop2cn.iapi.sqoptrade.testcase;

import java.math.BigDecimal;

import org.joda.time.DateTime;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.sqoptrade.service.AutoConfirmReceiveCall;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.SQPayGateWayCallService;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.database.sharding.db.sharding.model.AutoReceiveOptPo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.EnvSetup;

/**
 * 自动确认收货 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SqAutoConfirmReceive {

	private static AutoConfirmReceiveCall confirmreceivecall;
	
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("买家自动确认收货");
	}

	@Before
	public void caseUp() {

		confirmreceivecall = new AutoConfirmReceiveCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(confirmreceivecall.getOpurl());
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_AutoConfirmReceive_001() {
		Logger.start(true, "神器订单自动确认收货失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean);

			Long userId = placeOrderResp.getUserId();
			long goodsOwnerId = placeOrderResp.getOrderList().get(0)
					.getOrderPo().getGoodsOwnerId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);

			// 接单
			SqTradingCallService.sellerAcceptOrder(goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 神器订单发货
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId,
					1L, null);
			TradingSyncService.getSyncResult(orderId);

			//自动确认收货时间操作表（auto_receive_opt） 
			AutoReceiveOptPo record = new AutoReceiveOptPo();
			record.setOrderId(orderId);
			record.setExpectReceiveTime(DateTime.now().plusDays(-1).toDate());

			//修改预期自动确认收货时间为昨天
			orderShardingWapper.autoReceiveOptMapper
					.updateByPrimaryKeySelective(record);

			Logger.comment("前置数据准备结束");

			// 发送请求
			confirmreceivecall.setData("{}");
			confirmreceivecall.callService();

			Logger.verifyEquals("ok", confirmreceivecall.getReturnData(),
					"验证接口返回");

			Thread.sleep(5000);
			
			// 订单表(order)
			OrderPo ymtOrders = orderShardingWapper.selectOrderbyOrderId(orderId);

			Logger.comment(String.format("验证订单表(order)表 orderId: %s", orderId));

			Logger.verifyEquals(3, ymtOrders.getOrderStatus(), "验证orderStatus");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
